
var findgzkfldata = webrootpath+"gzkcate/findgzkfldata";  //获取所有分类
var insertgzkfl = webrootpath+"gzkcate/insertgzkfl";   //新增分类
var deletegzkflbylmid = webrootpath+"gzkcate/deletegzkflbylmid"; //删除分类

var indexgzkgladdbl = webrootpath+"gzkproperties/indexgzkgladdbl";//添加变量
var selectrulepropertiesbycateidlink = webrootpath+"gzkproperties/selectrulepropertiesbycateidlink";//获取分类下的属性信息
var deleteSXlink = webrootpath+"gzkproperties/deleteSXlink";
var model_gzkfl_updatebl_submit_link = webrootpath+"gzkproperties/model_gzkfl_updatebl_submit_link";//修改变量信息

var getgzrulelistidlink = webrootpath+"gz/getgzrulelistidlink"; //获取新增规则集id
var findgzjlistlink = webrootpath+"gz/findgzjlistlink";//获取规则集列表
var startrulelistlink = webrootpath+"gz/startrulelistlink";//启用规则集
var initrulelistlink = webrootpath+"gz/initrulelistlink";//执行应用规则集
var rule_script_testlink = webrootpath+"gzkproperties/rule_script_testlink";//测试规则集
var stoprulelistlink = webrootpath+"gz/stoprulelistlink";//停用规则集
var deleterulelistlink = webrootpath+"gz/deleterulelistlink";//删除规则集
var dumprulelistlink = webrootpath+"gz/dumprulelistlink";//导出规则集

var findservicepathlink = webrootpath+"gzkproperties/findservicepathlink";//获取接口根地址
var gzkflservicepublink = webrootpath+"gzkcate/gzkflservicepublink";//发布规则库分类对外接口
var creategzktemplatelink = webrootpath+"gzkproperties/creategzktemplatelink";//生成规则库变量模板

var index_gzkgl_addgz_frame = "./editrule.html";//打开添加规则编辑页面
var opensearchruleslink = "./rulelist/rulelistview.html";//查看规则集内容

var upload_file = webrootpath+"file/upload_file"; //上传文件
var delAttached_link = webrootpath+"doc/delAttached_link"; //删除附件
var import_excelrulelink = webrootpath+"gz/import_excelrulelink";//批量导入规则集文件
var uploader = "";

$(function () {
    index_lm_tab(0);
    findlmdata_link_fun(-1);

    $('#rule_doc_container').jsonViewer("");


});

/***
 * 栏目导航
 * @param _index
 */
function index_lm_tab(_index) {
    $("#index_lm_ul li").removeClass("active");
    $("#index_lm_cont_0").hide();
    $("#index_lm_cont_1").hide();
    $("#index_lm_cont_2").hide();
    $("#index_lm_cont_3").hide();
    if(_index=="0"){
        $("#index_lm_ul li:eq(0)").addClass("active");
        $("#index_lm_cont_0").show();


    }else if (_index=="1"){
        $("#index_lm_ul li:eq(1)").addClass("active");
        $("#index_lm_cont_1").show();
        findgzjlist();
    }else if (_index=="2"){
        $("#index_lm_ul li:eq(2)").addClass("active");

        $("#index_lm_cont_2_content").find("tr").each(function (_index,_this) {
            $(_this).find("td:eq(3)").find("input[type='text']").val("");
        });


        $("#index_lm_cont_2").show();
        $('#rule_doc_container').jsonViewer("");

    }else if (_index=="3"){
        $("#index_lm_ul li:eq(3)").addClass("active");
        $("#server_api_token").val("");
        $("#index_lm_cont_3").show();
        findservicepath();
    }
}

/***
 * 生成token
 */
function createServiceToken() {
    var apitoken = randomString(64);

    $("#server_api_token").val(apitoken);
}
function randomString(length) {
    var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var result = '';
    for (var i = length; i > 0; --i)
        result += str[Math.floor(Math.random() * str.length)];
    return result;
}


/***
 * 执行批量导入
 */
function gzkgl_add_import() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    var att_name_list = "";
    $("input[name='att_name']").each(function (_index,_this) {
        att_name_list+=","+_this.value;
    });
    if(att_name_list==""){
        return false;
    }
    $.post(import_excelrulelink,{
        selectcateid:selectcateid,
        att_name_list:att_name_list
    },function (content) {
        if(content.code=="1"){
            $("#import_excelrule_dialog").modal('hide');
            $.alert({content:"批量导入成功。"});
            findgzjlist();
            return false;

        }
    });
}


/***
 * 生成规则导入模板
 */
function gzkgl_create_templage() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    $.get(creategzktemplatelink,{
        selectcateid:selectcateid
    },function (content) {
        if(content.code=='1'){
            window.open(content.data);
        }
    });
}
/***
 * 接口发布
 */
function gzkflservicepub() {
    var gzkfladdress = $.trim($("#gzkfladdress").val());
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;

    var apitoken = $.trim($("#server_api_token").val());
    if(apitoken==""){
        $.alert({content:"请生成token。"});
        return false;
    }
    if(gzkfladdress==""){
        $.alert({content:"请填写接口内容。"});
        return false;
    }
    $.post(gzkflservicepublink,{
        gzkfladdress:gzkfladdress,
        selectcateid:selectcateid,
        apitoken:apitoken
    },function (content) {
         var code = content.code;
         if(code=="1"){
             $.alert({content:"接口发布成功"});
             $("#gzkfl_service_status").text("已发布");
         }else {
             $.alert({content:"接口发布失败"});
         }
    });
}
/***
 * 获取接口根路径及属性
 */
function findservicepath() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    $.post(findservicepathlink,{
        selectcateid:selectcateid
    },function (content) {
        if(content.code=="1"){
            var ruleapiservice = content.data.ruleapiservice;
            var apiname = content.data.apiname;
            var apistatus = content.data.apistatus;
            var apitoken = content.data.apitoken;
            $("#findservicepath_id").html(ruleapiservice);
            $("#gzkfladdress").val(apiname);
            $("#server_api_token").val(apitoken);
            if(apistatus=="10"){
                $("#gzkfl_service_status").text("已发布");
            }
        }else {
            $("#findservicepath_id").html("无法获取接口根路径");
        }
    });
}
/***
 * 执行应用规则集
 */
function initrulelist() {
    var ts = $.confirm({
        title: '提示',
        content: '您确定生成该分类下的规则脚本，并执行应用规则集吗？',
        buttons: {
            '确定': function () {
                var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
                var nodes = treeObj.getSelectedNodes();
                var selectcateid =  nodes[0].cateid;
                $.get(initrulelistlink,{
                    selectcateid:selectcateid
                },function (content) {
                    if(content.code=="1"){
                        $.alert({content: '应用成功，可以去脚本测试查看效果啦！'});
                    }else {
                        $.alert({content:content.msg});
                    }
                });
            },
            '取消': function () {
                ts.close();
            }
        }
    });
    
}
/***
 * 启用规则集
 * @param _listid
 */
function startrulelist(_listid) {
    $.get(startrulelistlink,{
        listid:_listid
    },function (content) {
        if(content.code=="1"){
            $.alert({content: '启用成功'});
            findgzjlist();
        }else {
            $.alert({content: '启用失败'});
        }
    });

}
/***
 * 获取分类下的规则集列表
 */
function findgzjlist() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    $.get(findgzjlistlink,{
        selectcateid:selectcateid
    },function (content) {
        if(content.code=="1"){
            var htmlstr = "";
            var datalist = content.data;
            for(var i in datalist){
                var dataobj = datalist[i];
                var liststatusstr = "未启用";
                if(dataobj.liststatus==20){
                    liststatusstr = "已启用";
                }
                htmlstr +="<tr>" +
                                "<td><input type='checkbox' />"+(parseInt(i)+1)+"</td>" +
                                "<td>"+dataobj.listname+"</td>" +
                                "<td>"+dataobj.listdesc+"</td>" +
                                "<td>"+liststatusstr+"</td>" +
                                "<td>"+dataobj.crdate+"</td>" +
                                "<td>"+dataobj.rulecount+"</td>" +
                                "<td>" +
                                    "<button type=\"button\" class=\"btn btn-info\" onclick='openSearchRules(\""+dataobj.listid+"\")' >查看</button>" +
                                    /*"<button type=\"button\" style='margin-left: 10px;' class=\"btn btn-info\" onclick='dumprulelist(\""+dataobj.listid+"\")'>导出</button>" +*/
                                    "<button type=\"button\" style='margin-left: 10px;' class=\"btn btn-success\" onclick='startrulelist(\""+dataobj.listid+"\")'>启用</button>" +
                                    "<button type=\"button\" style='margin-left: 10px;' class=\"btn btn-info\"     onclick='updaterulelist(\""+dataobj.listid+"\")'>修改</button>" +
                                    "<button type=\"button\" style='margin-left: 10px;' class=\"btn btn-danger\" onclick='stoprulelist(\""+dataobj.listid+"\")'>停用</button>" +
                                    "<button type=\"button\" style='margin-left: 10px;' class=\"btn btn-danger\" onclick='deleterulelist(\""+dataobj.listid+"\")'>删除</button>" +
                                "</td>" +
                          "</tr>";
            }
            $("#index_lm_doc_content").html(htmlstr);
        }else {
            $("#index_lm_doc_content").html("暂无内容");
        }

    });
}

/****
 * 导出规则集
 * @param _listid
 */
function dumprulelist(_listid) {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    var ts = $.confirm({
        title: '提示',
        content: '您确定导出规则集吗？',
        buttons: {
            '确定': function () {
                $.get(dumprulelistlink,{
                    listid:_listid,
                    cateid:selectcateid
                },function (msg) {
                    if(msg.code=="1"){

                    }
                });
            },
            '取消': function () {
                ts.close();
            }
        }
    });



}
/***
 * 删除规则集
 * @param _listid
 */
function deleterulelist(_listid) {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    var ts = $.confirm({
        title: '提示',
        content: '您确定删除此规则集吗？',
        buttons: {
            '确定': function () {
                $.get(deleterulelistlink,{
                    listid:_listid,
                    cateid:selectcateid
                },function (msg) {
                    if(msg.code=="1"){
                        $.alert("删除此规则集成功","提示");
                        findgzjlist();
                    }
                });
            },
            '取消': function () {
                ts.close();
            }
        }
    });
}
/***
 * 停用规则集
 * @param _listid
 */
function stoprulelist(_listid) {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;

    var ts = $.confirm({
        title: '提示',
        content: '您确定停用此规则集吗？',
        buttons: {
            '确定': function () {
                $.get(stoprulelistlink,{
                    listid:_listid,
                    cateid:selectcateid
                },function (msg) {
                    if(msg.code=="1"){
                        $.alert("停用此规则集成功","提示");
                        findgzjlist();
                    }
                });
            },
            '取消': function () {
                ts.close();
            }
        }
    });
}

/***
 * 修改规则集
 */
function updaterulelist(_listid) {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    sessionStorage.setItem("rulelistid",_listid);
    sessionStorage.setItem("selectcateid",selectcateid);

    window.open(index_gzkgl_addgz_frame);
}


/***
 * 查看规则集
 * @param _listid
 */
function openSearchRules(_listid) {
    sessionStorage.setItem("opensearchlistid",_listid);
    window.open(opensearchruleslink);
}
/***
 * 获取分类数据
 */
function findlmdata_link_fun(_node) {
    $.get(findgzkfldata,{node:_node},function (data) {
        if (data.code=='1'){
            initTree(data.data);
        }
    });
}

/***
 * 初始化树结构
 */
function initTree(_treedata) {
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        data:{
            keep:{
                parent:true
            }
        },
        callback:{
            onRightClick:function (event, treeId, treeNode) {
                zTreeObj.selectNode(treeNode);
                var clientx = event.clientX-210;
                var clienty = event.clientY-50;

                $("#index_lm_youjian_cateid").html(treeNode.cateid);
                $("#index_lm_youjian").css({"margin-left":clientx,"margin-top":clienty,"display":"block"});

            },
            onExpand:function (event, treeId, treeNode) {

                console.log(treeNode);
                var zAsync = treeNode.zAsync;
                //展开
                var treenodeid = treeNode.cateid;
                $.get(findgzkfldata,{node:treenodeid},function (data) {
                    if (data.code=='1'){
                        var sonnode = data.data.datalist;

                        zTreeObj.addNodes(treeNode, sonnode);
                    }
                });
            },
            onCollapse:function (event, treeId, treeNode) {
                //折叠后
                var treenodeid = treeNode.cateid;
                zTreeObj.removeChildNodes(treeNode);  

            },
            onClick:function (event, treeId, treeNode,clickFlag) {
                //点击选择
                index_lm_tab(0);
                selectrulepropertiesbycateid(treeNode.cateid);


            }
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    zTreeObj = $.fn.zTree.init($("#index_lm_tree"), setting, _treedata.datalist);
    var nodes = zTreeObj.getNodes();
    zTreeObj.selectNode(nodes[0]);
    index_lm_tab(0);
    selectrulepropertiesbycateid(0);
}

/***
 * 添加节点弹出窗口
 */
function index_lm_youjian_addnode() {
    index_lm_youjian_unb();
    $("#model_gzkfl_nodename").val("");
    $("#model_gzkfl_nodedesc").val("");
    $("#model_gzkfl").modal('show');
}

/***
 * 栏目删除节点
 */
function index_lm_youjian_deletenode() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var r=confirm("您确定删除此栏目吗？");
    if (r==true){
        $.get(deletegzkflbylmid,{cateid:nodes[0].cateid},function (msg) {
            if(msg.code=='1'){
                alert("删除成功");
                treeObj.removeNode(nodes[0]);
            }
        });
    }

}


/***
 * 提交更改
 */
function model_gzkfl_model_submit() {
    var model_lm_nodename = $.trim($("#model_gzkfl_nodename").val());
    var model_lm_nodedesc = $.trim($("#model_gzkfl_nodedesc").val());
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();


    $.post(insertgzkfl,{node_name:model_lm_nodename,node_desc:model_lm_nodedesc,selectlmid:nodes[0].cateid},function (data) {
        if (data.code=="1"){
            $("#model_gzkfl").modal('hide');
            //findlmdata_link_fun(data.data);
            $.get(findgzkfldata,{node:data.data},function (data) {
                if (data.code=='1'){
                    var selecttreeobj = $.fn.zTree.getZTreeObj("index_lm_tree");
                    var nodes = selecttreeobj.getSelectedNodes();
                    selecttreeobj.removeChildNodes(nodes[0]);
                    selecttreeobj.addNodes(nodes[0], data.data.datalist);

                }
            });
        }
    });
}

/***
 * 取消右键菜单
 */
function index_lm_youjian_unb() {
    $("#index_lm_youjian").css("display","none");
}
function index_lm_youjian_over() {
    $("#index_lm_youjian").css("display","block");
}

/***
 * 添加变量弹出框
 */
function gzkgl_add_bianliang() {
    $("#model_gzkfl_addbl_name").val("");
    $("#model_gzkfl_addbl_name_desc").val("");
    $("#model_gzkfl_addbl").modal('show');
}

/***
 * 添加变量
 */
function model_gzkfl_addbl_submit(){
    var model_gzkfl_addbl_name = $.trim($("#model_gzkfl_addbl_name").val());
    var model_gzkfl_addbl_content = $.trim($("#model_gzkfl_addbl_content").val());
    var model_gzkfl_addbl_name_desc = $.trim($("#model_gzkfl_addbl_name_desc").val());
    var model_gzkfl_addbl_type = $.trim($("#model_gzkfl_addbl_type").val());

    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes(); 
    $.post(indexgzkgladdbl,{
        selectlmid:nodes[0].cateid,
        model_gzkfl_addbl_name:model_gzkfl_addbl_name,
        model_gzkfl_addbl_content:model_gzkfl_addbl_content,
        model_gzkfl_addbl_name_desc:model_gzkfl_addbl_name_desc,
        model_gzkfl_addbl_type:model_gzkfl_addbl_type
    },function (data) {
        if (data.code=="1"){
            $("#model_gzkfl_addbl").modal('hide');
            selectrulepropertiesbycateid(nodes[0].cateid);

        }else {
            $.alert(data.msg,"提示");
        }
    });
}

/***
 * 执行测试脚本
 */
function rule_script_test() {
    $('#rule_doc_container').jsonViewer("");
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    var keyvalarraystr = "";
    $("#index_lm_cont_2_content").find("tr").each(function (_index,_this) {
        var keys = $(_this).find("td:eq(0)").text()+"";
        var vals = $(_this).find("td:eq(3)").find("input[type='text']").val()+"";
        keyvalarraystr+="|"+keys+"^"+encodeURI(vals);
    });
    if (keyvalarraystr==0){
        $.alert({
            title: '提示',
            content: '请最少填写一个变量值！'
        });
        return false;
    }
    keyvalarraystr = keyvalarraystr.substring(1);

    $.post(rule_script_testlink,{
        selectcateid:selectcateid,
        keyvalarraystr:keyvalarraystr
    },function (content) {
        $('#rule_doc_container').show();
        if(content.code=="1"){
            console.log("********")
            console.log(content);
            $('#rule_doc_container').jsonViewer(content, {collapsed: false, withQuotes: true, withLinks: false});
        }else {
            //$.alert({content: '脚本执行失败'});
            $('#rule_doc_container').jsonViewer(content, {collapsed: false, withQuotes: true, withLinks: false});
        }
    });
}
/***
 * 根据分类id获取属性信息
 * @param _selectcateid
 */
function selectrulepropertiesbycateid(_selectcateid) {
    $.get(selectrulepropertiesbycateidlink,{selectcateid:_selectcateid},function (content) {
        if(content.code=="1"){
           var datalist =  content.data;
            var htmlstr = "";
            var htmlscriptstr = "";
           for(var i in datalist){
                var dataobj = datalist[i];
                var propertiestypeval = "";
                if(dataobj.propertiestype=='10'){
                    propertiestypeval = "条件";
                    htmlscriptstr+="<tr>" +
                                        "<td><a href='javascript:void(0);' >"+dataobj.propertiesname+"</a></td>" +
                                        "<td>"+dataobj.propertiescontent+"</td>" +
                                        "<td>"+propertiestypeval+"</td>" +
                                        "<td><input type=\"text\" class=\"form-control\" id=\"gzk_tj_val\" placeholder=\"请输入值\"></td>" +
                                    "</tr>";
                }else if (dataobj.propertiestype=='20'){
                    propertiestypeval = "结果";
                }
               htmlstr+="<tr>" +
                                "<td><a href='javascript:void(0);' >"+dataobj.propertiesname+"</a></td>" +
                                "<td>"+dataobj.propertiescontent+"</td>" +
                                "<td>"+propertiestypeval+"</td>" +
                                "<td>"+dataobj.propertiesdesc+"</td>" +
                                "<td>" +
                               "<button type=\"button\" class=\"btn btn-danger\"  onclick='deleteSX(\""+dataobj.propertiesid+"\")'>删除</button>" +
                               "<button type=\"button\" style='margin-left: 20px;' onclick='updatesx(\""+dataobj.propertiesid+"\",\""+dataobj.propertiesname+"\",\""+dataobj.propertiescontent+"\",\""+dataobj.propertiestype+"\",\""+dataobj.propertiesdesc+"\")' class=\"btn btn-info\" >修改</button>" +
                               "</td>" +
                           "</tr>";
           }
           $("#index_lm_cont_0_content").html(htmlstr);

           $("#index_lm_cont_2_content").html(htmlscriptstr);

        }else {
            $("#index_lm_cont_0_content").html(content.msg);
        }
    });
}

/***
 * 修改属性信息提交
 */
function model_gzkfl_updatebl_submit() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var model_gzkfl_updatebl_id = $.trim($("#model_gzkfl_updatebl_id").val());
    var model_gzkfl_updatebl_name = $.trim($("#model_gzkfl_updatebl_name").val());
    var model_gzkfl_updatebl_content = $.trim($("#model_gzkfl_updatebl_content").val());
    var model_gzkfl_updatebl_type = $.trim($("#model_gzkfl_updatebl_type").val());
    var model_gzkfl_updatebl_name_desc = $.trim($("#model_gzkfl_updatebl_name_desc").val());
    $.post(model_gzkfl_updatebl_submit_link,{
        selectcateid:nodes[0].cateid,
        model_gzkfl_updatebl_id:model_gzkfl_updatebl_id,
        model_gzkfl_updatebl_name:model_gzkfl_updatebl_name,
        model_gzkfl_updatebl_content:model_gzkfl_updatebl_content,
        model_gzkfl_updatebl_type:model_gzkfl_updatebl_type,
        model_gzkfl_updatebl_name_desc:model_gzkfl_updatebl_name_desc
    },function (data) {
        if (data.code=="1"){
            $("#model_gzkfl_updatebl").modal('hide');
            selectrulepropertiesbycateid(nodes[0].cateid);
        }else {
            $.alert(data.msg,"提示");
        }
    });
}
/***
 * 修改属性信息
 */
function updatesx(_propertiesid,
                  _propertiesname,
                  _propertiescontent,
                  _propertiestype,
                  _propertiesdesc) {
    $("#model_gzkfl_updatebl_id").val(_propertiesid);
    $("#model_gzkfl_updatebl_name").val(_propertiesname);
    $("#model_gzkfl_updatebl_content").val(_propertiescontent);
    $("#model_gzkfl_updatebl_type").val(_propertiestype);
    $("#model_gzkfl_updatebl_name_desc").val(_propertiesdesc);
    $("#model_gzkfl_updatebl").modal('show');
/*
    $("#model_gzkfl_addbl_name").val("");
    $("#model_gzkfl_addbl_name_desc").val("");
    $("#model_gzkfl_addbl").modal('show');
*/

}
/***
 * 删除属性
 * @param _propertiesid
 */
function deleteSX(_propertiesid) {
    $.confirm({
        title:"提示",
        content:"您确定删除此属性吗？",
        buttons:{
            "确定":function () {
                $.get(deleteSXlink,{propertiesid:_propertiesid},function (msg) {
                    if (msg.code=='1'){
                        var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
                        var nodes = treeObj.getSelectedNodes();
                        selectrulepropertiesbycateid(nodes[0].cateid);
                    }
                });
            },
            "取消":function () {

            }
        }
    });




}
/***
 * 添加规则集
 */
function index_gzkgl_addgz() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    sessionStorage.setItem("selectcateid",selectcateid);
    $.get(getgzrulelistidlink,function (content) {
        if(content.code=='1'){
            sessionStorage.setItem("rulelistid",content.data);
            window.open(index_gzkgl_addgz_frame);
        }
    });
}





/****
 * 批量导入规则 弹出框
 */
function import_excelrule() {
    $("#import_excelrule_dialog").modal('show');
    setTimeout(function () {
        $("#picker_gzj").html("选择附件");
        loadUpladGZJ();
    },500);
}

/***
 * 批量导入规则（加载上传文件）
 */
function  loadUpladGZJ() {
    $("#thelist_uploadfile_gzj").html("");
    $("#thelist_gzj").html("");

    uploader = WebUploader.create({
        swf: '../webuploader-0.1.5/Uploader.swf',
        auto:true,
        server: upload_file,
        pick: '#picker_gzj',
        resize: false,
        fileNumLimit:1,
        formData:{
            filetype:10
        },
        accept:{
            title: '文本内容',
            extensions: 'xlsx',
            mimeTypes: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        },
        fileSizeLimit:10485760000
    });
    uploader.on( 'fileQueued', function( file ) {

        $("#thelist_gzj").html( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
            '</div>' );
    });
    uploader.on( 'beforeFileQueued', function( file ) {
        uploader.reset();
    });

    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
                '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                '</div>' +
                '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中并正在处理，请稍等！！！');
        $percent.css( 'width', percentage * 100 + '%' );
    });
    //上传成功
    uploader.on( 'uploadSuccess', function( file,response ) {
        $( '#'+file.id ).find('p.state').text('上传完毕');
        setTimeout(function(){
            $("#thelist").html("");
            if(response.code=='1'){
                var htmlstr = "<tr id=\"att"+response.data.attachedid+"\" >"+
                    "<td>"+response.data.truename+"<input name='att_name' value='"+response.data.attachedid+"' style='display: none;' /></td>"+
                    "<td>"+response.data.size+"</td>"+
                    "<td><span class=\"label label-danger\" style='cursor: pointer;'  onclick=\"delAttached('"+response.data.attachedid+"')\">删除</span></td>"+
                    "</tr>";

                $("#thelist_uploadfile_gzj").append(htmlstr);
            }
            $("#thelist_gzj").html("");
            uploader.reset();
        },1000);
    });
    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
        uploader.reset();
    });
    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });
}

/***
 *  删除附件
 */
function delAttached(attachedid) {
    $.confirm({
        title:"提示",
        content:"您确认删除此附件吗？",
        buttons:{
            "确定":function () {
                $.get(delAttached_link,{attachedid:attachedid},function (msg) {
                    if (msg.code=='1'){
                        $("#att"+attachedid).remove();
                        $.confirm({
                            title: '提示',
                            content: '成功删除附件！',
                            autoClose: '确定|2000',
                            buttons: {
                                确定: {
                                }
                            }
                        });
                    }
                });
            },
            "取消":function () {

            }
        }
    });
}
